﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SubSonic;

namespace AppBox.admin
{
    public partial class dept : PageBase
    {
        private static readonly log4net.ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            btnNew.OnClientClick = Window1.GetShowReference("~/admin/dept_new.aspx", "新增部门");

            BindGrid();
        }

        private void BindGrid()
        {
            SqlQuery q = new Select().From<XDept>();


            q.OrderAsc(XDept.SortIndexColumn.ColumnName);
            XDeptCollection depts = q.ExecuteAsCollection<XDeptCollection>();

            List<MyDept> mys = XDeptHelper.GetMyDeptCollection(depts);

            Grid1.DataSource = mys;
            Grid1.DataBind();
        }

        #endregion

        #region Events

        protected void Grid1_RowCommand(object sender, ExtAspNet.GridCommandEventArgs e)
        {
            int id = Convert.ToInt32(Grid1.DataKeys[e.RowIndex][0]);

            if (e.CommandName == "Delete")
            {
                XDept.Delete(id);

                BindGrid();
            }
        }

        protected void Window1_Close(object sender, EventArgs e)
        {
            BindGrid();
        }
        #endregion

    }
}
